/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.karelun.ideaboard.gef.ui.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.karelun.ideaboard.gef.EditPart;

/**
 * Superclass for an action needing the current selection.
 */
public class SelectionAction extends EditAction {
	
	protected Command command;

	private ISelectionProvider provider;
	
	private ISelection selection;
	

	public SelectionAction(IWorkbenchPart part, int style) {
		super(part, style);
	}

	public SelectionAction(IWorkbenchPart part) {
		super(part);
	}
	
	public SelectionAction(IEditorPart editor){
		super(editor);
	}
	
	@Override
	protected boolean canExecutable() {
		command=createCommand();
		return command.canExecute();
	}
	
	protected Command createCommand() {
		return UnexecutableCommand.INSTANCE;
	}
	
	@Override
	public void run() {
		getCommandStack().execute(command);
	}
	
	public void dispose() {
		this.selection = StructuredSelection.EMPTY;
		super.dispose();
	}

	protected List<?> getSelectedEditParts() {
		if (!(getSelection() instanceof IStructuredSelection))
			return Collections.EMPTY_LIST;
		return ((IStructuredSelection) getSelection()).toList();
	}
	
	protected Collection<Object> getSelectedEditPartModels(){
		List<?> selectedObjects = getSelectedEditParts();
		Collection<Object> collection=new ArrayList<Object>();
		for(Object object:selectedObjects){
			if(object instanceof EditPart){
				collection.add(((EditPart)object).getModel());
			}
		}
		return collection;
	}

	protected ISelection getSelection() {
		return selection;
	}

	protected void setSelection(ISelection selection) {
		this.selection = selection;
		handleSelectionChanged();
	}
	
	protected void handleSelectionChanged() {
		refresh();
	}

	public void setSelectionProvider(ISelectionProvider provider) {
		this.provider = provider;
	}

	public void update() {
		if (provider != null)
			setSelection(provider.getSelection());
		else
			setSelection(getWorkbenchPart().getSite().getWorkbenchWindow()
					.getSelectionService().getSelection());
	}
	
}
